package com.fasteat.util;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 该类用于记录已连接的用户和管道之间的映射
 */
public class UserChannelMap {
    private static ConcurrentHashMap<String, Channel> userChannelMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<ChannelId, String> channelIdStringMap = new ConcurrentHashMap<>();

    public static void put(String userId, Channel channel){
        userChannelMap.put(userId, channel);
        channelIdStringMap.put(channel.id(), userId);
    }

    public static Channel getChannel(String userId){
        return userChannelMap.get(userId);
    }

    public static String getUserId(ChannelId channelId){
        return channelIdStringMap.get(channelId);
    }

    public static String remove(ChannelId channelId){
        String removeUserId = channelIdStringMap.remove(channelId);
        userChannelMap.remove(removeUserId);
        return removeUserId;
    }
}
